﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DBBase;
using System.Threading;
using System.Xml;
using System.IO;

using System.Web;
using System.Runtime.InteropServices;
using System.IO.Ports;
using System.Security;
using System.Security.Cryptography;
namespace Business
{
    public partial class SecurityAudit : Form
    {
        public MainForm m_mainform;
        public SecurityAudit()
        {
            InitializeComponent();
        }
        private string Encryption(string express)//加密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] plaindate = Encoding.Default.GetBytes(express);
                byte[] encryptdate = rsa.Encrypt(plaindate, false);

                return Convert.ToBase64String(encryptdate);
            }
        }
        //
        private string Decrypt(string cip)//解密
        {
            CspParameters param = new CspParameters();
            param.KeyContainerName = "gzwk2006";
            using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(param))
            {
                byte[] encryptdate = Convert.FromBase64String(cip);
                byte[] decryptdate = rsa.Decrypt(encryptdate, false);
                return Encoding.Default.GetString(decryptdate);
            }
        }
        public void getjilu()
        {
            lv.Items.Clear();
            Query mquery = new Query();
            string sql;
            sql = string.Format(@"Select * from anquanriji Where aqrj_date>'{0}' and aqrj_date<'{1}'", dateTimePicker1.Value.ToShortDateString() + " 00:00:00", dateTimePicker2.Value.ToShortDateString() + " 23:59:59");

            sql = sql + " order by aqrj_id desc ";
            //保存操作日记
            m_mainform.savecaozuoriji(sql, "查询", "业务系统", "查询安全日志");

      

            //try
            {
                DataTable dt = mquery.GetTable(sql);

                if (dt.Rows.Count > 0)
                {
                    
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {

    

                        ListViewItem lt = new ListViewItem(string.Format("{0}", i + 1));
                        lt.SubItems.Add(dt.Rows[i]["aqrj_yh"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["aqrj_sj"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["aqrj_yhip"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["aqrj_yhlx"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["aqrj_date"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["aqrj_mokuai"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["aqrj_sfhx"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["aqrj_sffcg"].ToString().Trim());
                        lt.SubItems.Add(dt.Rows[i]["aqrj_jg"].ToString().Trim());
                        
                        string rjnr = dt.Rows[i]["aqrj_miaoshu"].ToString().Trim();
                        try
                        {
                            rjnr = Decrypt(dt.Rows[i]["aqrj_miaoshu"].ToString().Trim());
                        }
                        catch
                        {
                            rjnr = rjnr + "*";
                        }
                        lt.SubItems.Add(rjnr);
                      
                        this.lv.Items.Add(lt);
                       
                    }
                    foreach (ListViewItem lvItems in lv.Items)
                    {
                        if (lvItems.SubItems[10].Text.Contains("*"))
                        {
                            lvItems.ForeColor = Color.Red;//字体颜色
                            m_mainform.baocunaqrj("数据篡改", "写入", "安全数据库", "否", "否", "成功", string.Format("安全数据库的{0}数据被篡改", lvItems.SubItems[0].Text));//事件、类型（增、删、该、查）、模块、是否核心功能、是否非常规业务、结果和描述

                        }
                        else
                        {
                            lvItems.ForeColor = Color.Black;//字体颜色
                        }
                    }
                }


            }



        }

        private void but_query_Click(object sender, EventArgs e)
        {
            lv.Visible = true;
            lv2.Visible = false;
            //判断是否核心功能
            if (m_mainform.shxgn.Contains("安全日志查询"))
            {
                m_mainform.baocunhxrj("安全日志查询", string.Format("安全日志查询"),"查询");// 核心功能日记  日记内容
            }
            getjilu();
        }

        private void but_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void SecurityAudit_Load(object sender, EventArgs e)
        {
            //判断是否民警
            if (m_mainform.sfjy.Contains("安全日志查询"))
            {
                if (!m_mainform.SFMJ.Contains("是"))
                {
                    but_query.Enabled = false;
                }
            }

            //if (!m_mainform.squanxian.Contains("安全日志查询"))
            //{
            //    but_query.Enabled = false;
            //}

            //if (!m_mainform.squanxian.Contains("安全日志统计"))
            //{
            //    but_tongji.Enabled = false;
            //}
            if (!m_mainform.squanxian.Contains("安全日志导出"))
            {
                skinButton1.Enabled = false;
            }
        }

        private void but_tongji_Click(object sender, EventArgs e)
        {
            lv2.Visible = true;
            lv.Visible = false;
            tongji();
        }
        public void tongji()
        {
            lv2.Items.Clear();
            Query mquery = new Query();
            string sql;
            sql = string.Format(@" select aqrj_yh from anquanriji group by aqrj_yh having count(aqrj_yh)>0 ");//用户名
            DataTable dt = mquery.GetTable(sql);

            
            var name = "";
            var sdate = "";
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    
                    name =name+ dt.Rows[i]["aqrj_yh"].ToString().Trim()+":";
                    sql = string.Format(@"select * from anquanriji where aqrj_yh='{0}' 
                                            and aqrj_date>'{1}' ",
                                            dt.Rows[i]["aqrj_yh"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:",idate);
                    //ListViewItem lt = new ListViewItem(string.Format("{0}", icount + 1));
                    //lt.SubItems.Add(string.Format("用户名:{0}", name));
                    //lt.SubItems.Add(string.Format("{0}", idate));
                    //if (idate > 0)
                    //{
                    //    this.lv2.Items.Add(lt);
                    //    icount++;
                    //}
                }
            }
            string[] cname_yh = name.Split(new char[] { ':' });
            string[] cdate_yh = sdate.Split(new char[] { ':' });
             name = "";
             sdate = "";
            sql = string.Format(@" select aqrj_sj from anquanriji group by aqrj_sj having count(aqrj_sj)>0 ");//事件
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    name = name + dt.Rows[i]["aqrj_sj"].ToString().Trim() + ":";
                    sql = string.Format(@"select * from anquanriji where aqrj_sj='{0}' 
                                            and aqrj_date>'{1}' ",
                                            dt.Rows[i]["aqrj_sj"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_sj = name.Split(new char[] { ':' });
            string[] cdate_sj = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";
            sql = string.Format(@" select aqrj_yhip from anquanriji group by aqrj_yhip having count(aqrj_yhip)>0 ");//aqrj_yhip
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["aqrj_yhip"].ToString().Trim() + ":";
                  //  name = dt.Rows[i]["aqrj_yhip"].ToString().Trim();
                    sql = string.Format(@"select * from anquanriji where aqrj_yhip='{0}' 
                                            and aqrj_date>'{1}' ",
                                            dt.Rows[i]["aqrj_yhip"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_yhip = name.Split(new char[] { ':' });
            string[] cdate_yhip = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";
            sql = string.Format(@" select aqrj_yhlx from anquanriji group by aqrj_yhlx having count(aqrj_yhlx)>0 ");//aqrj_yhlx
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["aqrj_yhlx"].ToString().Trim() + ":";
                    //name = dt.Rows[i]["aqrj_yhlx"].ToString().Trim();
                    sql = string.Format(@"select * from anquanriji where aqrj_yhlx='{0}' 
                                            and aqrj_date>'{1}' ",
                                            dt.Rows[i]["aqrj_yhlx"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }

            string[] cname_yhlx = name.Split(new char[] { ':' });
            string[] cdate_yhlx = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";
            sql = string.Format(@" select aqrj_mokuai from anquanriji group by aqrj_mokuai having count(aqrj_mokuai)>0 ");//aqrj_mokuai
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["aqrj_mokuai"].ToString().Trim() + ":";
                   // name = dt.Rows[i]["aqrj_mokuai"].ToString().Trim();
                    sql = string.Format(@"select * from anquanriji where aqrj_mokuai='{0}' 
                                            and aqrj_date>'{1}' ",
                                            dt.Rows[i]["aqrj_mokuai"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_mokuai = name.Split(new char[] { ':' });
            string[] cdate_mokuai = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";
            sql = string.Format(@" select aqrj_sfhx from anquanriji group by aqrj_sfhx having count(aqrj_sfhx)>0 ");//aqrj_mokuai
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["aqrj_sfhx"].ToString().Trim() + ":";
                  //  name = dt.Rows[i]["aqrj_sfhx"].ToString().Trim();
                    sql = string.Format(@"select * from anquanriji where aqrj_sfhx='{0}' 
                                            and aqrj_date>'{1}' ",
                                            dt.Rows[i]["aqrj_sfhx"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_sfhx = name.Split(new char[] { ':' });
            string[] cdate_sfhx = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";
            sql = string.Format(@" select aqrj_sffcg from anquanriji group by aqrj_sffcg having count(aqrj_sffcg)>0 ");//aqrj_sffcg
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["aqrj_sffcg"].ToString().Trim() + ":";
                  //  name = dt.Rows[i]["aqrj_sffcg"].ToString().Trim();
                    sql = string.Format(@"select * from anquanriji where aqrj_sffcg='{0}' 
                                            and aqrj_date>'{1}' ",
                                            dt.Rows[i]["aqrj_sffcg"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_sffcg = name.Split(new char[] { ':' });
            string[] cdate_sffcg = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";

            sql = string.Format(@" select aqrj_jg from anquanriji group by aqrj_jg having count(aqrj_jg)>0 ");//aqrj_jg
            dt = mquery.GetTable(sql);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    name = name + dt.Rows[i]["aqrj_jg"].ToString().Trim() + ":";
                    //name = dt.Rows[i]["aqrj_jg"].ToString().Trim();
                    sql = string.Format(@"select * from anquanriji where aqrj_jg='{0}' 
                                            and aqrj_date>'{1}' ",
                                            dt.Rows[i]["aqrj_jg"].ToString().Trim(),
                                            dateTimePicker1.Value.ToShortDateString() + " 00:00:00");
                    var idate = 0;
                    idate = mquery.selectDBcount(sql);
                    sdate = sdate + string.Format("{0}:", idate);
                }
            }
            string[] cname_jg = name.Split(new char[] { ':' });
            string[] cdate_jg = sdate.Split(new char[] { ':' });
            name = "";
            sdate = "";

            var count = 0;
            if (count < cname_yh.Length)
            {
                count = cname_yh.Length;
            }
            if (count < cname_sj.Length)
            {
                count = cname_sj.Length;
            }
            if (count < cname_yhip.Length)
            {
                count = cname_yhip.Length;
            }
            if (count < cname_yhlx.Length)
            {
                count = cname_yhlx.Length;
            }
            if (count < cname_mokuai.Length)
            {
                count = cname_mokuai.Length;
            }
            if (count < cname_sfhx.Length)
            {
                count = cname_sfhx.Length;
            }
            if (count < cname_sffcg.Length)
            {
                count = cname_sffcg.Length;
            }
            if (count < cname_jg.Length)
            {
                count = cname_jg.Length;
            }
            count = count - 1;
            for (int i = 0; i < count; i++)
            {
                ListViewItem lt = new ListViewItem(string.Format("{0}", i + 1));
                if (i < cname_yh.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_yh[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_yh[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }
                //2
                if (i < cname_sj.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_sj[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_sj[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }
                //3
                if (i < cname_yhip.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_yhip[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_yhip[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }

                //4
                if (i < cname_yhlx.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_yhlx[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_yhlx[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }

                //5
                if (i < cname_mokuai.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_mokuai[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_mokuai[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }

                //6
                if (i < cname_sfhx.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_sfhx[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_sfhx[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }
                //7
                if (i < cname_sffcg.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_sffcg[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_sffcg[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }
                //8
                if (i < cname_jg.Length)
                {
                    lt.SubItems.Add(string.Format("{0}", cname_jg[i]));
                    lt.SubItems.Add(string.Format("{0}", cdate_jg[i]));
                }
                else
                {
                    lt.SubItems.Add("");
                    lt.SubItems.Add("");
                }
                
                    this.lv2.Items.Add(lt);
                   
            }

        }

        private void skinButton1_Click(object sender, EventArgs e)
        {
            lv2.Visible = true;
            lv.Visible = false;
            tongji();

            string path = "";
            path = Application.StartupPath + path + DateTime.Now.ToString("yyyyMMdd") + ".xls";
            excel(path);
        }
        private void excel(string path)
        {
            try
            {
                StreamWriter sw = new StreamWriter(path, false, Encoding.GetEncoding("gb2312"));
                StringBuilder sb = new StringBuilder();

                sb.Append("统计时间从" + dateTimePicker1.Value.ToShortDateString() + "至" + DateTime.Now.ToString("yyyy-MM-dd") + "\t");
                sb.Append(Environment.NewLine);
                for (int i = 0; i < lv2.Columns.Count; i++)
                    sb.Append(lv2.Columns[i].Text.ToString() + "\t");
                sb.Append(Environment.NewLine);

                foreach (ListViewItem li in lv2.Items)
                {
                    for (int i = 0; i < lv2.Columns.Count; i++)
                        sb.Append(li.SubItems[i].Text + "\t");
                    sb.Append(Environment.NewLine);
                }
                sw.Write(sb.ToString());
                sw.Flush();
                sw.Close();
                MessageBox.Show("成功保存于" + path + "文件！");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
